﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using GPS.GPS_DAL;

namespace GPS.Controllers
{
    public class GPS_LichHocController : FDBController
    {

        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }

        //
        // GET: /GPS_LichHoc/
        public ActionResult Index(ViewModelDKH_LH SearchModel)
        {
            SearchModel.KH_LH_DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            initialCategorySearchAction(SearchModel);

            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_KH_LH(SearchModel.KH_LH_DVGTVT_MA
                                                                                , SearchModel.KH_LH_KH_MA
                                                                                , SearchModel.KH_LH_GV_MA
                                                                                , SearchModel.KH_LH_PT_BIENSO
                                                                                , SearchModel.HANGDT_MA
                                                                                );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }

        public ActionResult Create()
        {
            initialCategoryCreateAction();
            CrudModelDKH_LH model = new CrudModelDKH_LH();
            string _dvgt_ma = this.getCurrentUser().MA_TINHTP;
            // var hvs = _db.Filter<DHV>(o => o.HV_DVGTVT_MA == _dvgt_ma).Take(10).ToList();
            model.ListHVs = new List<SelectItemDHV>();
            //  hvs.ForEach(o => model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
            //get GV thuoc ĐVĐT
            //getCurrentUser

            //GET PT thuộc ĐVĐT



            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CrudModelDKH_LH model)
        {
            if (ModelState.IsValid)
            {
                if (!this.IsBooking(model))
                {
                    var _isMoreThan = this.IsMoreTime(model);
                    if (_isMoreThan == 1)
                    {
                        ViewBag.ErrorDate = "(*)Giáo viên không được quá 12 giờ 1 ngày!";
                        var hvs = _db.Filter<DHV>(o => o.HV_KH_MA == model.KH_LH_KH_MA).ToList();
                        model.ListHVs = new List<SelectItemDHV>();
                        hvs.ForEach(o => model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
                    }
                    else if (_isMoreThan == 2)
                    {
                        ViewBag.ErrorDate = "(*)Phương tiện không được quá 12 giờ 1 ngày!";
                        var hvs = _db.Filter<DHV>(o => o.HV_KH_MA == model.KH_LH_KH_MA).ToList();
                        model.ListHVs = new List<SelectItemDHV>();
                        hvs.ForEach(o => model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
                    }
                    else
                    {
                        DKH_LH _model = new DKH_LH();
                        GPS.COMMON.Helpers.CopyObject<DKH_LH>(model, ref _model, new List<string> { "KH_LH_ACTIVE", "SelectedHVs", "SelectItemDHV" });
                        _model.KH_LH_ACTIVE = "1";

                        //get max lich học
                        _model.ID = EFSequence.GetNextValue("SEQ_DKH_LH_ID");
                        _model.KH_LH_NGUOINHAP = User.Identity.Name;
                        _model.KH_LH_NGAYNHAP = DateTime.Now;
                        _db.Insert<DKH_LH>(_model);

                        //Insert HV vào phương tiện:

                        if (!string.IsNullOrWhiteSpace(_model.KH_LH_HTDT_MA) && _model.KH_LH_HTDT_MA != "LT")
                        {
                            if (model.SelectedHVs != null && model.SelectedHVs.Length > 0)
                            {
                                DKH_PT _pthvModel = null;
                                for (int i = 0; i < model.SelectedHVs.Length; i++)
                                {
                                    _pthvModel = new DKH_PT();
                                    _pthvModel.ID = EFSequence.GetNextValue("SEQ_DKH_PT_ID");
                                    _pthvModel.KH_PT_KH_LH_ID = _model.ID;
                                    _pthvModel.KH_PT_PT_MA = _model.KH_LH_PT_BIENSO;
                                    _pthvModel.KH_PT_DVGTVT_MA = _model.KH_LH_DVGTVT_MA;
                                    _pthvModel.KH_PT_HV_MA = model.SelectedHVs[i];
                                    _pthvModel.KH_PT_KH_MA = _model.KH_LH_KH_MA;
                                    _pthvModel.KH_PT_NGAYNHAP = DateTime.Now;
                                    _pthvModel.KH_PT_NGUOINHAP = User.Identity.Name;
                                    _db.Insert<DKH_PT>(_pthvModel);
                                }

                            }
                        }

                        return RedirectToAction("Index");
                    }

                }
                else
                {
                    ViewBag.ErrorDate = "(*)Phương tiện và giáo viên không được trùng lịch học!";
                    var hvs = _db.Filter<DHV>(o => o.HV_KH_MA == model.KH_LH_KH_MA).ToList();
                    model.ListHVs = new List<SelectItemDHV>();
                    hvs.ForEach(o => model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
                }

            }
            initialCategoryCreateAction();

            return View(model);
        }

        public ActionResult Edit(decimal id)
        {

            DKH_LH model = _db.GetOne<DKH_LH>(o => o.ID == id);

            CrudModelDKH_LH _model = new CrudModelDKH_LH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LH>(model, ref _model, new List<string> { "KH_LH_ACTIVE", "SelectedHVs", "SelectItemDHV" });
            //get học viên thuộc khóa học
            string _KH_LH_KH_MA = model.KH_LH_KH_MA;
            var query =
                     from kh in _db.GetDbSet<DKH>()
                     join hv in _db.GetDbSet<DHV>() on kh.KH_MA equals hv.HV_KH_MA
                     where kh.KH_MA == _KH_LH_KH_MA
                     select new { hv.HV_MA, hv.HV_TEN, hv.HV_HODEM };

            _model.ListHVs = new List<SelectItemDHV>();
            query.ToList().ForEach(o => _model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));

            //Get học viên thuộc phương tiện/
            var _lstSeleceted = _db.Filter<DKH_PT>(o => o.KH_PT_KH_LH_ID == id);
            if (_lstSeleceted != null && _lstSeleceted.Count > 0)
            {
                _model.SelectedHVs = new string[_lstSeleceted.Count];
                int i = 0;
                _lstSeleceted.ForEach(
                    p =>
                    {
                        _model.SelectedHVs.SetValue(_lstSeleceted[i].KH_PT_HV_MA, i);
                        i++;
                    }
                    );
            }

            initialCategoryEditAction(_model);
            return View(_model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDKH_LH model)
        {
            if (ModelState.IsValid)
            {
                if (!this.IsBooking(model))
                {
                    var _isMoreThan = this.IsMoreTime(model);
                    if (_isMoreThan == 1)
                    {
                        ViewBag.ErrorDate = "(*)Giáo viên không được quá 12 giờ 1 ngày!";
                        var hvs = _db.Filter<DHV>(o => o.HV_KH_MA == model.KH_LH_KH_MA).ToList();
                        model.ListHVs = new List<SelectItemDHV>();
                        hvs.ForEach(o => model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
                    }
                    else if (_isMoreThan == 2)
                    {
                        ViewBag.ErrorDate = "(*)Phương tiện không được quá 12 giờ 1 ngày!";
                        var hvs = _db.Filter<DHV>(o => o.HV_KH_MA == model.KH_LH_KH_MA).ToList();
                        model.ListHVs = new List<SelectItemDHV>();
                        hvs.ForEach(o => model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
                    }
                    else
                    {
                        var _model = _db.GetOne<DKH_LH>(o => o.ID == model.ID);
                        GPS.COMMON.Helpers.CopyObject<DKH_LH>(model, ref _model, new List<string> { "KH_LH_ACTIVE", "SelectedHVs", "SelectItemDHV" });

                        _db.Update<DKH_LH>(_model);

                        //Insert HV vào phương tiện:

                        if (!string.IsNullOrWhiteSpace(_model.KH_LH_HTDT_MA) && _model.KH_LH_HTDT_MA != "LT")
                        {
                            if (model.SelectedHVs != null && model.SelectedHVs.Length > 0)
                            {
                                //delete xong insert:
                                _db.Delete<DKH_PT>(o => o.KH_PT_KH_LH_ID == model.ID);

                                DKH_PT _pthvModel = null;
                                for (int i = 0; i < model.SelectedHVs.Length; i++)
                                {
                                    _pthvModel = new DKH_PT();
                                    _pthvModel.ID = EFSequence.GetNextValue("SEQ_DKH_PT_ID");
                                    _pthvModel.KH_PT_KH_LH_ID = _model.ID;
                                    _pthvModel.KH_PT_PT_MA = _model.KH_LH_PT_BIENSO;
                                    _pthvModel.KH_PT_DVGTVT_MA = _model.KH_LH_DVGTVT_MA;
                                    _pthvModel.KH_PT_HV_MA = model.SelectedHVs[i];
                                    _pthvModel.KH_PT_KH_MA = _model.KH_LH_KH_MA;
                                    _pthvModel.KH_PT_NGAYNHAP = DateTime.Now;
                                    _pthvModel.KH_PT_NGUOINHAP = User.Identity.Name;
                                    _pthvModel.KH_PT_NGAYSUA = DateTime.Now;
                                    _pthvModel.KH_PT_NGUOISUA = User.Identity.Name;
                                    _db.Insert<DKH_PT>(_pthvModel);
                                }

                            }
                        }

                        return RedirectToAction("Index");
                    }

                }
                else
                {
                    ViewBag.ErrorDate = "(*)Phương tiện và giáo viên không được trùng lịch học!";
                    var hvs = _db.Filter<DHV>(o => o.HV_KH_MA == model.KH_LH_KH_MA).ToList();
                    model.ListHVs = new List<SelectItemDHV>();
                    hvs.ForEach(o => model.ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
                }

            }
            initialCategoryEditAction(model);
            return View(model);
        }

        public ActionResult Delete(decimal id)
        {
            _db.Delete<DKH_PT>(o => o.KH_PT_KH_LH_ID == id);

            _db.Delete<DKH_LH>(o => o.ID == id);

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Delete(CrudModelDKH_LOP_LH model)
        {
            return View();
        }

        #region detail
        public ActionResult Detail(decimal id)
        {

            DKH_LH model = _db.GetOne<DKH_LH>(o => o.ID == id);

            CrudModelDKH_LH _model = new CrudModelDKH_LH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LH>(model, ref _model);

            initialCategoryDetailAction(_model);
            return PartialView("_Detail", _model);
        }
        public void initialCategoryDetailAction(CrudModelDKH_LH view)
        {
            ViewBag.dvgtvt = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == view.KH_LH_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            ViewBag.khoahocs = _db.Filter<DKH>(o => o.KH_MA == view.KH_LH_KH_MA).Select(x => x.KH_TEN).FirstOrDefault();

            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            var htdt = _db.Filter<SHTDAOTAO>(o => o.HTDT_MA == view.KH_LH_HTDT_MA).Select(x => x.HTDT_MA).FirstOrDefault();
            view.KH_LH_HTDT_MA = htdt.ToString();


            ViewBag.KH_LOP_LH_GDOAN_MAs = _db.Filter<SGIAIDOAN>(x => x.GDOAN_MA == view.KH_LH_GDOAN_MA).Select(x => x.GDOAN_MA).FirstOrDefault();

            ViewBag.PT_MAs = CategoryCommon.DEFAULT_VALUE_DDL;
            if (view.KH_LH_HTDT_MA != null && view.KH_LH_HTDT_MA != "LT")
            {
                ViewBag.PT_MAs = _db.Filter<DPT>(o => o.PT_DVGTVT_MA == view.KH_LH_DVGTVT_MA).Select(x => x.PT_BIENSO).FirstOrDefault();
            }

            //GV
            var gvs = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == view.KH_LH_DVGTVT_MA).Select(
               r => new
               {
                   GV_MA = r.GV_MA,
                   GV_TEN = r.GV_HODEM + " " + r.GV_TEN,
               }).OrderBy(r => r.GV_MA).FirstOrDefault();
            ViewBag.GV_MAs = gvs;

        }

        public ActionResult HocVienPhuongTien(string id, string id2)
        {
            string _KH_LH_PT_MA = id2.Replace("%3A", ":");
            var query =
                      from kh_pt in _db.GetDbSet<DKH_HV1>()
                      join hv in _db.GetDbSet<DHV>() on kh_pt.KH_HV_HV_MA equals hv.HV_MA
                      where (kh_pt.KH_HV_KH_MA == id && kh_pt.KH_HV_PT_MA == _KH_LH_PT_MA)
                      select new
                      {
                          hv.ID,
                          hv.HV_MA,
                          hv.HV_DVGTVT_MA,
                          hv.HV_KH_MA,
                          hv.HV_HANGDT_MA,
                          hv.HV_HANGDT_RANK,
                          hv.HV_HODEM,
                          hv.HV_TEN,
                          hv.HV_GIOITINH,
                          hv.HV_QUOCTICH_MA,
                          hv.HV_NGAYSINH,
                          hv.HV_NOITT,
                          hv.HV_NOITT_DVHC_MA,
                          hv.HV_NOITT_DVHC_DVQL,
                          hv.HV_NOICT,
                          hv.HV_NOICT_DVHC_MA,
                          hv.HV_NOICT_DVHC_DVQL,
                          hv.HV_SOCMT,
                          hv.HV_NGAYCAPCMT,
                          hv.HV_NOICAPCMT,
                          hv.HV_MOTA,
                          hv.HV_ACTIVE,
                          hv.HV_NGAYNHAP,
                          hv.HV_NGUOINHAP,
                          hv.HV_NGAYSUA,
                          hv.HV_NGUOISUA
                      };
            List<DHV> entities = new List<DHV>();
            foreach (var hv in query)
            {

                entities.Add(new DHV  //Added UsageCPU  
                {
                    ID = hv.ID,
                    HV_MA = hv.HV_MA,
                    HV_DVGTVT_MA = hv.HV_DVGTVT_MA,
                    HV_KH_MA = hv.HV_KH_MA,
                    HV_HANGDT_MA = hv.HV_HANGDT_MA,
                    HV_HANGDT_RANK = hv.HV_HANGDT_RANK,
                    HV_HODEM = hv.HV_HODEM,
                    HV_TEN = hv.HV_TEN,
                    HV_GIOITINH = hv.HV_GIOITINH,
                    HV_QUOCTICH_MA = hv.HV_QUOCTICH_MA,
                    HV_NGAYSINH = hv.HV_NGAYSINH,
                    HV_NOITT = hv.HV_NOITT,
                    HV_NOITT_DVHC_MA = hv.HV_NOITT_DVHC_MA,
                    HV_NOITT_DVHC_DVQL = hv.HV_NOITT_DVHC_DVQL,
                    HV_NOICT = hv.HV_NOICT,
                    HV_NOICT_DVHC_MA = hv.HV_NOICT_DVHC_MA,
                    HV_NOICT_DVHC_DVQL = hv.HV_NOICT_DVHC_DVQL,
                    HV_SOCMT = hv.HV_SOCMT,
                    HV_NGAYCAPCMT = hv.HV_NGAYCAPCMT,
                    HV_NOICAPCMT = hv.HV_NOICAPCMT,
                    HV_MOTA = hv.HV_MOTA,
                    HV_ACTIVE = hv.HV_ACTIVE,
                    HV_NGAYNHAP = hv.HV_NGAYNHAP,
                    HV_NGUOINHAP = hv.HV_NGUOINHAP,
                    HV_NGAYSUA = hv.HV_NGAYSUA,
                    HV_NGUOISUA = hv.HV_NGUOISUA
                }

                );


            }
            ViewBag.PT_BIENSO = id2;
            return PartialView("_HocVienPhuongTien", entities);
        }

        #endregion

        #region"Ajax"
        [HttpPost]
        public ActionResult getKH_MAs(string ma_DVGT)
        {
            var districts = _db.Filter<DKH>(d => d.KH_DVGTVT_MA == ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_TEN + "</option>");

            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getGV_MAs(string Ma_DVGT, string ma_KH)
        {
            var _HANGDT_RANK = _db.GetOne<DKH>(o => o.KH_MA == ma_KH).KH_HANGDT_RANK;

            var districts = _db.Filter<DGV>(d => d.GV_DVGTVT_MA == Ma_DVGT && (_HANGDT_RANK == null || d.GV_HANGDT_RANK > _HANGDT_RANK))
                                .Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");
            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getPT_MAs(string Ma_DVGT, string ma_HT, string ma_LOP)
        {

            var districts = _db.Filter<DPT>(d => d.PT_DVGTVT_MA == Ma_DVGT).Select(a => "<option value='" + a.PT_BIENSO + "'>" + a.PT_BIENSO + "</option>");

            return Content(string.Join("", districts));
        }


        //get Học viên
        [HttpPost]
        public ActionResult getHV_MAs(string Ma_DVGT, string ma_KH, string ma_LOP)
        {
            List<ModelDHV> _lst = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GetdataHocVienInLop(Ma_DVGT, ma_KH, ma_LOP);
            var districts = _lst.Select(a => "<option value='" + a.HV_MA + "'>" + a.HV_HODEM + " " + a.HV_TEN + "</option>"); ;

            return Content(string.Join("", districts));
        }

        //getHV_BY_KH_MAs
        public ActionResult getHV_BY_KH_MAs(string Ma_KH)
        {
            List<SelectItemDHV> ListHVs = new List<SelectItemDHV>();
            var hvs = _db.Filter<DHV>(o => o.HV_KH_MA == Ma_KH);
            hvs.ForEach(o => ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
            var districts = ListHVs.Select(a => "<option value='" + a.HV_MA + "'>" + a.HV_HOTEN + "</option>"); ;

            //  return Json(ListHVs);
            return Content(string.Join("", districts));
        }
        #endregion

        #region Function



        private bool IsBooking(CrudModelDKH_LH _model)
        {
            var IsBooking = false;
            if (_model != null)
            {
                IsBooking = _db.EntityExists<DKH_LH>(
                b => (b.KH_LH_GV_MA == _model.KH_LH_GV_MA || b.KH_LH_PT_BIENSO == _model.KH_LH_PT_BIENSO)
                    && b.KH_LH_TUNGAY < _model.KH_LH_DENNGAY && b.KH_LH_DENNGAY > _model.KH_LH_TUNGAY && b.ID != _model.ID
                );

            }


            return IsBooking;
        }

        private int IsMoreTime(CrudModelDKH_LH _model)
        {

            var isMoreTime = -1;
            TimeSpan diff = _model.KH_LH_DENNGAY.Value - _model.KH_LH_TUNGAY.Value;
            decimal deltaDate = Convert.ToDecimal(diff.TotalMinutes);
            var totalTimeGV = deltaDate;

            //check giảng viên
            var sogioGV = Singleton<GPS_HocVienDAO>.Inst.GPS_GET_SOGIOHOC_GVPT(_model.KH_LH_DVGTVT_MA, _model.KH_LH_GV_MA, string.Empty, _model.KH_LH_TUNGAY.Value.Date);
            if (sogioGV != null)
            {
                totalTimeGV = totalTimeGV + sogioGV.HOURS.Value;
            }
            if (totalTimeGV > 720)
            {
                isMoreTime = 1;
                return 1;
            }


            //check phương tiện
            if (_model.KH_LH_HTDT_MA != "LT")
            {
                var totalTimePT = deltaDate;
                var sogioPT = Singleton<GPS_HocVienDAO>.Inst.GPS_GET_SOGIOHOC_GVPT(_model.KH_LH_DVGTVT_MA, string.Empty, _model.KH_LH_PT_BIENSO, _model.KH_LH_TUNGAY.Value.Date);
                if (sogioPT != null)
                {
                    totalTimePT = totalTimePT + sogioPT.HOURS.Value;
                }
                if (totalTimePT > 720)
                {
                    isMoreTime = 2;
                    return 2;
                }


            }

            return isMoreTime;

        }

        public void initialCategorySearchAction(ViewModelDKH_LH viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => (o.DVGTVT_MAQL == viewModel.KH_LH_DVGTVT_MA) || (o.DVGTVT_MA == viewModel.KH_LH_DVGTVT_MA)), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN(viewModel.KH_LH_DVGTVT_MA);  //_db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.KH_LH_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            var htdts = _db.GetAll<SHTDAOTAO>();
            //  ViewBag.HTDT_MAs = new SelectList(htdts, "HTDT_MA", "HTDT_MOTA");
            ViewBag.HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(o => o.HANGDT_RANK > 4), "HANGDT_MA", "HANGDT_TEN");
        }

        public void initialCategoryCreateAction()
        {
            ApplicationUser curUser = getCurrentUser();
            string _madvgtvt = curUser.MA_TINHTP;

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == _madvgtvt), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.HTDT_MAs = new SelectList(_db.GetAll<SHTDAOTAO>(), "HTDT_MA", "HTDT_MOTA");
            ViewBag.GD_MAs = new SelectList(_db.GetAll<SGIAIDOAN>(), "GDOAN_MA", "GDOAN_MOTA");
            ViewBag.HANGHT = " ";
            ViewBag.HANGGPLX = " ";
            string ma_DVGT = this.getCurrentUser().MA_TINHTP;
            ViewBag.KH_MAs = new SelectList(Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN(ma_DVGT), "KH_MA", "KH_TEN");   //new SelectList(_db.Filter<DKH>(d => d.KH_DVGTVT_MA == ma_DVGT),"KH_MA","KH_TEN");
            var gvs = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == ma_DVGT).Select(
               r => new
               {
                   GV_MA = r.GV_MA,
                   GV_TEN = r.GV_HODEM + " " + r.GV_TEN,
               }).OrderBy(r => r.GV_MA).ToList();
            gvs.Insert(0, new
            {
                GV_MA = "",
                GV_TEN = "",
            });

            ViewBag.GV_MAs = new SelectList(gvs, "GV_MA", "GV_TEN");
            var pts = _db.Filter<DPT>(o => o.PT_DVGTVT_MA == ma_DVGT);
            pts.Insert(0, new DPT
            {
                PT_BIENSO = ""

            });
            ViewBag.PT_MAs = new SelectList(pts, "PT_BIENSO", "PT_BIENSO");
        }

        public void initialCategoryEditAction(CrudModelDKH_LH view)
        {

            // ApplicationUser curUser = getCurrentUser();
            ApplicationUser curUser = getCurrentUser();
            string _madvgtvt = curUser.MA_TINHTP;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == _madvgtvt), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.HTDT_MAs = new SelectList(_db.GetAll<SHTDAOTAO>(), "HTDT_MA", "HTDT_MOTA");
            ViewBag.GD_MAs = new SelectList(_db.GetAll<SGIAIDOAN>(), "GDOAN_MA", "GDOAN_MOTA");

            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN(view.KH_LH_DVGTVT_MA); //_db.Filter<DKH>(o => o.KH_DVGTVT_MA == view.KH_LH_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            //PT
            ViewBag.PT_MAs = CategoryCommon.DEFAULT_VALUE_DDL;
            if (view.KH_LH_HTDT_MA != null && view.KH_LH_HTDT_MA != "LT")
            {
                var pts = _db.Filter<DPT>(o => o.PT_DVGTVT_MA == view.KH_LH_DVGTVT_MA);
                ViewBag.PT_MAs = new SelectList(pts, "PT_BIENSO", "PT_BIENSO");
            }
            else
            {
                var pts = _db.Filter<DPT>(o => o.PT_DVGTVT_MA == view.KH_LH_DVGTVT_MA);
                ViewBag.PT_MAs = new SelectList(pts, "PT_BIENSO", "PT_BIENSO");
            }

            //GV
            if (view.KH_LH_HTDT_MA != null && view.KH_LH_HTDT_MA != "LT")
            {
                var gvs = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == view.KH_LH_DVGTVT_MA).Select(
                r => new
                {
                    GV_MA = r.GV_MA,
                    GV_TEN = r.GV_HODEM + " " + r.GV_TEN,
                }).OrderBy(r => r.GV_MA).ToList();
                ViewBag.GV_MAs = new SelectList(gvs, "GV_MA", "GV_TEN");
            }
            else
            {
                var gvs = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == view.KH_LH_DVGTVT_MA).Select(
                r => new
                {
                    GV_MA = r.GV_MA,
                    GV_TEN = r.GV_HODEM + " " + r.GV_TEN,
                }).OrderBy(r => r.GV_MA).ToList();
                ViewBag.GV_MAs = new SelectList(gvs, "GV_MA", "GV_TEN");
            }

        }
        #endregion
    }
}